package lagou.cn.homework

import scala.beans.BeanProperty
import scala.collection.mutable.ArrayBuffer

class Computer(_name:String) {
  @BeanProperty
   var name : String = _name

  @BeanProperty
   var score_ : Int = 0

  def showFist(): Int = {
    val num = 1 + scala.util.Random.nextInt(3)
    var fist = ""
    num match {
      case 1 => fist = "石头"
      case 2=> fist = "剪刀"
      case 3=> fist = "布"
    }
    println(s"$name 出拳 $fist")
    num
  }
}

class User(_name:String) {

  @BeanProperty
  protected var name : String = _name

  @BeanProperty
  protected var score : Int = _

  def showFist(): Int = {

    println("请出拳!1.石头;2.剪刀;3.布")
    val num = scala.io.StdIn.readInt()
    var fist = ""
    num match {
      case 1 => fist = "石头"
      case 2=> fist = "剪刀"
      case 3=> fist = "布"
      case _ =>
    }

    if (fist.equals("")) {
      println("输入不符合规范, 默认为布!")
      fist = "布"
    }

    println(s"你出拳:$fist")
    num
  }

}

class Game() {

  @BeanProperty
  var user:User = _
  @BeanProperty
  var computer: Computer = _
  @BeanProperty
  var gameTimes:Int = 0

  @BeanProperty
  var userSituation = new ArrayBuffer[Int]()
  @BeanProperty
  var computerSituation = new ArrayBuffer[Int]()


  def init(): Boolean ={

    println("***********欢迎进入游戏世界***********")
    println("************************************")
    println("************猜拳, 开始***************")
    println("************************************")
    println()
    println()
    println()

    var flag = true
    var rival = 0
    while (flag) {
      println("请选择对战角色:(1:刘备;2:关羽;3:张飞)")
      rival = scala.io.StdIn.readInt()
      if (rival == 1 || rival == 2 || rival == 3) {
        flag = !flag
      }
    }

    var computerName = ""
    rival match {
      case 1 => computerName = "刘备"
      case 2 => computerName = "关羽"
      case 3 => computerName = "张飞"
    }

    this.computer = new Computer(computerName)
    this.user = new User("你")
    println(s"你选择了与${computer.getName}对战")

    println("要开始么?(y/n)")
    val start = scala.io.StdIn.readLine()
    if (start.equalsIgnoreCase("y")) {
      return true;
    }
    println("退出游戏")
    false;
  }

  def continueGame():Boolean = {
    println("是否开始下一局游戏?(y/n)")
    val hasNext = scala.io.StdIn.readLine()
    if (hasNext.equalsIgnoreCase("y")) {
      return true
    }
    false
  }

  def userFist():Int = {
    this.user.showFist()
  }

  def computerFist():Int = {
    this.computer.showFist()
  }

}

object Test02 {



  def main(args: Array[String]): Unit = {

    val game = new Game()

    if (game.init()) {
      var flag:Boolean = true
      while(flag) {
        game.gameTimes += 1
        val userFist = game.userFist()
        val computerFist = game.computerFist()
        val minus = userFist - computerFist
        if (minus == 0) {
          game.userSituation += 1
          game.computerSituation += 1
          println("平局,继续努力!")
        } else if (minus == -1 || minus == 2) {
          game.userSituation += 2
          game.computerSituation += 0
          println("恭喜你, 获胜!")
        } else {
          game.userSituation += 0
          game.computerSituation += 2
          println("计算机获胜!")
        }

        flag = game.continueGame()
        if (!flag) {
          println("退出游戏!")
          println("-----------------------------------------")
          println(game.computer.getName() + " VS " + "游客")
          println(s"对战次数${game.gameTimes}次")
          println("姓名\t分数\t胜\t和\t负")
          print("游客\t" + game.userSituation.sum + "\t" +  game.userSituation.count(_ == 2) + "\t")
          print(game.userSituation.count(_ == 1) + "\t")
          print(game.userSituation.count(_ == 0))
          println()

          print(game.computer.getName() + "\t" + game.computerSituation.sum + "\t" + game.computerSituation.count(_ == 2) + "\t")
          print(game.computerSituation.count(_ == 1) + "\t")
          print(game.computerSituation.count(_ == 0))
          println()
        }
      }
    }
  }
}
